/***
 * Excerpted from "Hello, Android! 3e",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/eband3 for more book information.
 ***/
package org.me.countrycity.SQL;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import org.me.countrycity.utill.LetterToTable;

public class SQLHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "ContryCity.db";
    private static final int DATABASE_VERSION = 1;
    private static final int MIN_LETTER = 1488;
    private static final int MAX_LETTER = 1490;

    /** Create a helper object for the Events database */
    public SQLHelper(Context ctx) {
        super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        for (int i = MIN_LETTER; i < MAX_LETTER; i++) {
            db.execSQL("CREATE TABLE " + LetterToTable.getTable(i) + " ("
                    + Constants.WORD + " STRING NOT NULL , "
                    + Constants.CATAGORY + " STRING NOT NULL "
                    //+ "CONSTRAINT word_cat PRIMARY KEY (" + Constants.WORD + ")"// + "," + Constants.CATAGORY + ")"
                    + ");");
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Clear();
    }

    public void Clear() {
        SQLiteDatabase db = this.getWritableDatabase();
        for (int i = MIN_LETTER; i <= MAX_LETTER; i++) {
            db.execSQL("DROP TABLE IF EXISTS " + LetterToTable.getTable(i));
        }
        onCreate(db);
    }
}
